{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3086025b",
   "metadata": {},
   "source": [
    "---------\n",
    "*本学习记录人：陈依玟\n",
    "\n",
    "*学习时间：2021-11-16\n",
    "\n",
    "*week12\n",
    "\n",
    "*参考的文档：\n",
    "> 1. [Python的官方文档]（https://docs.python.org/3/） \n",
    "> 2. [许老师的博客]（https://www.cnblogs.com/xuzhichao/p/11407192.html）\n",
    "> 3. [pyecharts的官方文档] (https://pyecharts.org/#/)\n",
    "----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2c62711",
   "metadata": {},
   "source": [
    "## 本周学习内容\n",
    "\n",
    "### 1.认识列表（序列数据）\n",
    "\n",
    "### 2.列表的切片（index）和切片（slice）\n",
    "\n",
    "### 3.列表的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e0ba733",
   "metadata": {},
   "source": [
    "## 1.认识[列表]()\n",
    "\n",
    "*  最通用的是list,它可以写成括号之间以逗号分隔的值（项）的列表。列表可能包含不同类型的数据，但通常这些内容都具有相同的类型\n",
    "\n",
    "> 1.列表的形式：括号之间以逗号分隔的值： [value1,value2,value3...]\n",
    "> 2.列表可以 索引（index） 和切片 （slice）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0ec1cff",
   "metadata": {},
   "source": [
    "* 思考：如果我想存一些（一个班、一堆用户的名字），怎么存？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f7ceb9d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'LXT|'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用字符串来存 (操作起来--非常之不方便)\n",
    "stu_names = \"LXT|CYW|XM\"\n",
    "stu_names[0:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9d543262",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'LXT'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_names_list = [\"LXT\",\"CYW\",\"XM\"]\n",
    "stu_names_list[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50af2663",
   "metadata": {},
   "source": [
    "## 2.列表的切片（index）和切片（slice)\n",
    "```\n",
    " names = [\"LXT\",\"CYW\",\"XM\",\"XG\"]\n",
    "            0     1     2    3\n",
    "           -4    -3    -2   -1\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b271728f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['LXT', 'CYW', 'XM', 'XG']\n"
     ]
    }
   ],
   "source": [
    " names = [\"LXT\",\"CYW\",\"XM\",\"XG\"]\n",
    " \n",
    " print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec311637",
   "metadata": {},
   "source": [
    "> 1. 取值 按照索引取值\n",
    "\n",
    "> 2. 切片（slice）\n",
    ">> 1.letters[start:stop:step]\n",
    ">> 2.如果没有指定开始值，则默认为0；\n",
    ">> 3.如果没有指定结束指，则取列表允许的最大值；\n",
    ">> 4.如果没有指定步长值，则默认步长为1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5a60e1a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LXT CYW\n"
     ]
    }
   ],
   "source": [
    "print(names[0],names[-3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "58648879",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['CYW', 'XM']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a3f779a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['CYW', 'XM', 'XG']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[-3:]         # 后面不写取到最后一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3e0e71a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LXT', 'CYW', 'XM']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[:3]          # 取到前面不写取到最前面一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "585e6f79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LXT', 'CYW', 'XM', 'XG']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[:]           # 前后不写取全部的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8013af8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LXT', 'XM']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取 LXT 和 XM     *2 为步长\n",
    "names[0:3:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f14a5a31",
   "metadata": {},
   "source": [
    "## 3.列表的方法\n",
    "> 1. 列表的增加\n",
    "> 2. 列表的删除\n",
    "> 3. 列表的修改\n",
    "> 4. 列表的查询（如上：取值和切片）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "52bd3a4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['LXT', 'CYW', 'XM', 'XG']\n"
     ]
    }
   ],
   "source": [
    " names = [\"LXT\",\"CYW\",\"XM\",\"XG\"]\n",
    " \n",
    " print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "55e02fde",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)  \n",
    "#  因为列表可以存不同类型的数据（object）， 增加一个object到列表末尾"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a527cc0c",
   "metadata": {},
   "source": [
    "> 1.列表的增加\n",
    ">> 1.append()--> L(list).append(object) --> None -- append object to end\n",
    " \n",
    ">> 2. insert()---> L.insert(index,object) --->  insert object before index\n",
    "\n",
    "| 列表的方法  | 用法   |  details  |\n",
    "|-----|---|---|\n",
    "1| append()  | L(list).append(object) | None -- append object to end   |\n",
    "2|insert()|L.insert(index,object)| insert object before index|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2e7e64f",
   "metadata": {},
   "source": [
    " * 表格：|    |     |     |\n",
    " 随意竖线|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a822e6f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"zhuzhu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "92ce9aa1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LXT', 'CYW', 'XM', 'XG', 'zhuzhu']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "480b1c02",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fde039f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(2,\"BB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4ec44863",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LXT', 'CYW', 'BB', 'XM', 'XG', 'zhuzhu']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9150063b",
   "metadata": {},
   "source": [
    "> 2 .列表的删除\n",
    "\n",
    ">> 1. remove()--> L.remove(value) --> L.remove(value)-->(注意) Raises ValueError if the value is not present\n",
    ">> 2. pop() --> L.pop([index])  -->移除并返回移除的值 --> (注意)Raise IndexError if list is empty or index is out of range, 如果pop()index不写，将移除最后一个值\n",
    "\n",
    "|序号|列表的方法|用法|注意|\n",
    "|---------|---------|-------|-------|\n",
    "|3|remove()| L.remove(value)| L.remove(value)|Raises ValueError if the value is not present|\n",
    "|4| pop()| L.pop([index])|移除并返回移除的值 |Raise IndexError if list is empty or index is out of range, 如果pop()index不写，将移除最后一个值|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "24abb79d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)\n",
    "# 列表的 索引值（index） 和  值 （value）\n",
    "# 1.列表可以共同出现一些值\n",
    "# 2.共同出现的值，删除第一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e2e2d789",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 尝试增加一个共同的名字\n",
    "names.append('zhuzhu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7c810d6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LXT', 'CYW', 'BB', 'XM', 'XG', 'zhuzhu', 'zhuzhu']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "40db21eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.remove(\"zhuzhu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "531ff202",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LXT', 'CYW', 'BB', 'XM', 'XG', 'zhuzhu']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "deeb00fb",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-0266ea9a9ddd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 思考：   如果这个值不存在 remove  会怎样\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mnames\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"yiming\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "# 思考：   如果这个值不存在 remove  会怎样\n",
    "names.remove(\"yiming\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fbce5bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除remove使用的正确方法\n",
    "if 'yiming' in names:\n",
    "    names.remove(\"yiming\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df66cc0a",
   "metadata": {},
   "source": [
    "------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba9b2071",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对于初学者，help非常有用\n",
    "help(names.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e646338",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1adc4cf9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. pop()   index  不写:删除最后一个值，对比记忆 append（）增加最后一个值\n",
    "names.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebefaf46",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87e26797",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. index 正常写 pop():删除指定索引位置的值\n",
    "names.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c82eb965",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14ca52cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop index out of range\n",
    "names.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fadfe66",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"XM\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f8c8d1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41a6ea20",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"BB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bc1458d",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e16d8ea9",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42aec79d",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = 4\n",
    "if index < len(names):\n",
    "    names.pop(index)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2595fa9c",
   "metadata": {},
   "source": [
    "* pop删除索引值 eg:01234\n",
    "* value删除"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e6b02c8",
   "metadata": {},
   "source": [
    "> 5.查询数量 count()\n",
    "> 6.查看value的索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63f2aa0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares = [88,87,90,88,78,90,96,98,95,95,88]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70b060b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(squares.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b751a22d",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares.count(88)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fde5895",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(squares.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2658d252",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares.index(90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ab42749e",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'squares' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-6f1827b2f066>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msquares\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'squares' is not defined"
     ]
    }
   ],
   "source": [
    "squares.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da6ab14d",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5fcba3d",
   "metadata": {},
   "source": [
    "##  5.课后作业\n",
    "\n",
    "> 1.用表格总结列表的所有方法，如上表格所示\n",
    "> 2,课本第二章列表的三个项目实践，请同学们先做\n",
    ">> * 其中三个项目的知识点：1. 可能涉及用户输入 2.一定涉及列表的切片 3.一定涉及列表的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1361edac",
   "metadata": {},
   "outputs": [],
   "source": [
    "！pip install pyecharts# 安装 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c544366",
   "metadata": {},
   "source": [
    "|序号|列表的方法|用法|注意|\n",
    "|---------|---------|-------|-------|\n",
    "|1| append() | L(list).append(object) | None -- append object to end   |无|\n",
    "|2|insert()|L.insert(index,object)| insert object before index|无|\n",
    "|3|remove()| L.remove(value)| L.remove(value)|Raises ValueError if the value is not present|\n",
    "|4| pop()| L.pop([index])|移除并返回移除的值 |Raise IndexError if list is empty or index is out of range, 如果pop()index不写，将移除最后一个值|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af8e7f6e",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab158997",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(squares.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9351918b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
